package managedbeans.fileshare;

import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import langresources.ResourcesController;
import managedbeans.GenericForumBean;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import utils.dataaccess.IOUtil;

@ManagedBean
@RequestScoped
public class FileDownloadBean extends GenericForumBean implements Serializable{

    private StreamedContent fileStreamedContent;
    private ArrayList<String> fileList;

    File storageDir = new File(IOUtil.getProjectRoot()+"/storage");
    File file = null;

    public FileDownloadBean() {
        reloadFileList();
    }
    
    public void initTransferContent(String fileName) {
        if(fileName == null){
            showMessage(ResourcesController.extractResource("MESSAGE.WARNING"), ResourcesController.extractResource("FILE_NOT_SELECTED"));
            return;
        }
        file = new File(storageDir, fileName);
        InputStream stream = null;
        try {
            stream = new DataInputStream(new FileInputStream(file));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileDownloadBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        fileStreamedContent = new DefaultStreamedContent(stream, "application/binary", fileName);
    }
    
    public String removeFile(String fileName){
        IOUtil.deleteFile(fileName);
        return("FILE_DELETED");
    }
    
    public String getIcon(String fileName){
        String filePath;
        int lastDot = fileName.lastIndexOf(".")+1;
        String extension = fileName.substring(lastDot);
        if(lastDot==0 || !new File(IOUtil.getProjectRoot()+"images/icons/"+extension+".png").exists()){
            filePath = "images/icons/default.png";
        } else {
            filePath = "images/icons/"+extension+".png";
        } 
        return filePath;
    }
    
    public StreamedContent getFile(String fileName) {
        initTransferContent(fileName);
        return fileStreamedContent;
    }


    public void setFile(StreamedContent file) {
        this.fileStreamedContent = file;
    }
    
    public void setFile(File file) {
        this.file = file;
    }

    public ArrayList getFileList() {
        return fileList;
    }

    public void setFileList(ArrayList fileList) {
        this.fileList = fileList;
    }

    public StreamedContent getFileStreamedContent() {
        return fileStreamedContent;
    }

    public void setFileStreamedContent(StreamedContent fileStreamedContent) {
        this.fileStreamedContent = fileStreamedContent;
    }

    public File getStorageDir() {
        return storageDir;
    }

    public void setStorageDir(File storageDir) {
        this.storageDir = storageDir;
    }

    public void reloadFileList() {
        File[] storageContent = storageDir.listFiles();
        fileList = new ArrayList<String>();
        for(File file : storageContent){
              fileList.add(file.getName());
        }
    }
    
    
}
